<?php

/**
 * CelebrioMail default
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    CelebrioMail
 */

namespace ApplicationsModule\TalkerModule;

use WebtopModule\BasePresenter;
use OpenTok\OpenTokSDK;
use OpenTok\API_Config;
use Nette\Forms\Form;

class DefaultPresenter extends BasePresenter {

    public function startup() {
        parent::startup();
        header("Content-type:text/html; charset=utf-8"); // TODO: poresit
        if ($this->isAjax()) {
            $this->invalidateControl("celebriotalkerappSnippet");
        }
    }

    public function renderDefault() {
        
    }

    public function renderChatwindow() {
        $this->template->messageForm = $this["messageForm"];
        $openTok = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET);
        $this->template->openTokToken = $openTok->generate_token();
        $this->template->openTokAPIKey = API_Config::API_KEY;
        try {
            $this->template->openTokSessionID = (string) $openTok->create_session($_SERVER["REMOTE_ADDR"])->getSessionId();
            ;
        } catch (OpenTokException $e) {
            // TODO: error
        }
    }

    public function createComponentMessageForm() {
        $form = new \Nette\Application\AppForm();
        $form->addTextArea("message")
                ->addRule(Form::FILLED);
        $form->addSubmit('submited', _('Send'));
        $form->onSubmit[] = callback($this, 'composeSubmitted');
        return $form;
    }

}